<?php
class PersetujuanSuratKeluarController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column1', meaning
	 * using one-column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
		//	'rights',
		);
	}

	public function allowedActions() 
        { 
            return ''; 
        }

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new PersetujuanSuratKeluar;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['PersetujuanSuratKeluar']))
		{
			$model->attributes=$_POST['PersetujuanSuratKeluar'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id_persetujuan_surat_keluar));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
                
		if(isset($_POST['PersetujuanSuratKeluar']) || isset($_POST['SuratKeluar']))
		{
                    if(isset($_POST['PersetujuanSuratKeluar']['alasan_penolakan']))
                        $_POST['PersetujuanSuratKeluar']['setuju'] = 0;
                    else
                        $_POST['PersetujuanSuratKeluar']['setuju'] = 1;
                    
                    $model->attributes=$_POST['PersetujuanSuratKeluar'];
                    $model->waktu_persetujuan = date('Y-m-d H:i:s');
                    
                    if($model->save()){
                        
                        // ditolak
                        if(!$model->setuju)
                        { 
                             new SendMessage(
                                    Pegawai::model()->findByPk($model->SuratKeluar->id_pegawai), 
                                    array(
                                            'url'           => '/suratKeluar/update/'.$model->SuratKeluar->id_surat_keluar,
                                            'notifikasi'    => "Surat dengan nomor ".$model->SuratKeluar->no_surat." di tolak oleh ".Yii::app()->user->getName(),
                                            'status'        => 0
                                        )
                                    );
                        }
                        else
                        {
                            
                            $PersetujuanCriteria = new CDbCriteria;
                            $PersetujuanCriteria->order = 'sort ASC';
                            $PersetujuanCriteria->limit = 1;

                            $ListPersetujuan = PersetujuanSuratKeluar::model()->findByAttributes(
                                                    array(
                                                        'id_surat_keluar' => $model->id_surat_keluar,
                                                        'visible' => 0
                                                    ),
                                                    $PersetujuanCriteria
                                                );

                            if(!empty($ListPersetujuan))
                            {
                                $ListPersetujuan->visible = 1;
                                $ListPersetujuan->save();

                                new SendMessage(
                                                Pegawai::model()->findByPk($ListPersetujuan->id_pegawai), 
                                                array(
                                                        'url'           => '/persetujuanSuratKeluar/',
                                                        'notifikasi'    => "Surat dengan nomor ".$model->SuratKeluar->no_surat." membutuhkan persetujuan anda.",
                                                        'status'        => 0
                                                    )
                                                );  
                            }else{
                                // Persetujuan sudah habis

                                new SendMessage(
                                                Pegawai::model()->findByPk($model->SuratKeluar->id_pegawai), 
                                                array(
                                                        'url'           => '/suratKeluar/'.$model->SuratKeluar->id_surat_keluar,
                                                        'notifikasi'    => "Surat dengan nomor ".$model->SuratKeluar->no_surat." Telah di setujui.",
                                                        'status'        => 0
                                                    )
                                                );

                            }
                            
                        }
                            
                        $this->redirect(array('index'));
                    }
		}
                
                $approved = PersetujuanSuratKeluar::model()->count(
                                                    'id_surat_keluar='.$model->id_surat_keluar.
                                                    ' AND (setuju IS NULL)'
                                                );
                
		$this->render('update',array(
			'model'=>$model,
                        'approved' => $approved,
                        'SuratKeluar' => new SuratKeluar,
                        'Pegawai' => new Pegawai
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$model=new PersetujuanSuratKeluar('search');
                
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['PersetujuanSuratKeluar']))
			$model->attributes=$_GET['PersetujuanSuratKeluar'];

		$this->render('index',array(
			'model'=>$model,
                        'SuratKeluar' => new SuratKeluar,
                        'Pegawai' => new Pegawai
		));
	}

	

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=PersetujuanSuratKeluar::model()->with('Pegawai', 'SuratKeluar')->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='persetujuan-surat-keluar-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
